<?php
require_once('../../includes/classes/core.php');

$action	= $_REQUEST['act'];
$error	= '';
$data	= '';

switch ($action) {
	case 'get_edit_page':
		
 		$mac_id	 = $_REQUEST['id'];
 		
 		$page		 = GetPage(Getmonitoring($mac_id));
 		
 		$button_name = get_button_name($mac_id);
 		$data		 = array('page'	=> $page, 'button_name' => $button_name);

 		break;
	case 'get_list':
		$count		= $_REQUEST['count'];
		$hidden		= $_REQUEST['hidden'];
		$ip			= $_REQUEST['ip'];
		$status		= $_REQUEST['status'];
		if($status == 0){
			$fillter= " AND (UNIX_TIMESTAMP(NOW()) - monitoring_computer_state.live_datetime  > 20)";
		}else{
			$fillter="AND monitoring_computer_state.`status`=$status  AND (UNIX_TIMESTAMP(NOW()) - monitoring_computer_state.live_datetime  <= 20)";
		}
		
		
			
		$rResult = mysql_query("SELECT 	monitoring_computer_state.mac,
										object.object_name,
										monitoring_computer_state.computer_name,
										IF((UNIX_TIMESTAMP(NOW())- monitoring_computer_state.live_datetime) > 20, 'მიუწვდომელი',`status`.`status`) AS `status`,
										SEC_TO_TIME(UNIX_TIMESTAMP(NOW())- monitoring_computer_state.datetime),
										IF(ISNULL(url_name.url_name),url_name.url,url_name.url_name)
										
								FROM    monitoring_computer_state
								LEFT JOIN 	object ON object.ip = monitoring_computer_state.ip
								JOIN   `status` ON monitoring_computer_state.`status` = `status`.id
								JOIN   `url_name` ON monitoring_computer_state.`last_url` = `url_name`.url
								WHERE monitoring_computer_state.ip='$ip' $fillter
								");

		$data = array(
				"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) ){
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ ){
				if($i==3){
 					if 	   ($aRow[$i]=='დაკავებული'){$row[]='<div style="float: left; background: #E89B00; width: 16px; height: 16px; margin-top: 3px; margin-left: 7px;">&nbsp;</div><div style="margin-left: 39px;">'.$aRow[$i].'</div>';}
 					elseif ($aRow[$i]=='თავისუფალი'){$row[]='<div style="float: left; background: #52E800; width: 16px; height: 16px; margin-top: 3px; margin-left: 7px;">&nbsp;</div><div style="margin-left: 39px;">'.$aRow[$i].'</div>';}
 					elseif ($aRow[$i]=='მიუწვდომელი'){$row[]='<div style="float: left; background: #808080; width: 16px; height: 16px; margin-top: 3px; margin-left: 7px;">&nbsp;</div><div style="margin-left: 39px;">'.$aRow[$i].'</div>';}
 				    else   {$row[] = $aRow[$i];}	
 				}else{  
					$row[] = $aRow[$i];
				}
			}
			$data['aaData'][] = $row;
		}

		break;
	case 'close_screen':
		$mac_id	 		= $_REQUEST['id'];
		$refresh_dialog	= $_REQUEST['refresh_dialog'];
		
		if ($refresh_dialog==''){
			mysql_query("UPDATE monitoring_computer_state 
						 SET    screenshot_enabled = IF(screenshot_enabled=1,0,1)
						 WHERE  monitoring_computer_state.mac = '$mac_id'");
		}
		
		$button_name = get_button_name($mac_id);
		$page		 = GetPage(Getmonitoring($mac_id));
		$data		 = array('page'	=> $page, 'button_name'	=> $button_name);
		
		break;
	
	default:
		$error = 'Action is Null';
}
$data['error'] = $error;
echo json_encode($data);


/* 
 ******************************
 * Monitoring Functions
 * ******************************
*/
function get_button_name($mac_id){
	$res = mysql_fetch_assoc(mysql_query("SELECT  monitoring_computer_state.screenshot_enabled
										  FROM monitoring_computer_state
										  WHERE monitoring_computer_state.mac='$mac_id'"));
	if ($res['screenshot_enabled']==0){
		$dutton_name='სკრინშოტის ჩართვა';
	}else{
		$dutton_name='სკრინშოტის გამორთვა';
	}
	
	return $dutton_name;
}

function Getmonitoring($mac_id){
	  $res = mysql_fetch_assoc(mysql_query("SELECT  monitoring_computer_state.mac,
													monitoring_computer_state.screenshot_enabled,
													monitoring_computer_state.ip,
													monitoring_computer_state.computer_name,
													now() AS date_time,
													`status`.`status` AS `status`
											FROM 	monitoring_computer_state
											JOIN 	`status` ON `status`.id=monitoring_computer_state.status
											WHERE 	monitoring_computer_state.mac='$mac_id'" ));
	return $res;
}


function GetPage($res = ''){
	$res1 = mysql_query("SELECT monitoring_screenshots.image
						 FROM   monitoring_screenshots
						 WHERE  monitoring_screenshots.mac = '$res[mac]'
						 ORDER BY monitoring_screenshots.id DESC
						 LIMIT 1");
	
	if (mysql_num_rows($res1) == 0 || $res['screenshot_enabled']==0){
		$check = 'style="display: none";';
	}
	
	$data = '
			<div id="dialog-form">
			<table class="dialog-form-table">
				<tr>
					<td style="width: 200px;">თარიღი: <span style="font-weight: bold;" >'. $res['date_time'] . '</span></td>
					<td style="width: 200px;">სახელი: <span style="font-weight: bold;" >' . $res['computer_name'] . '</span></td>
					<td style="width: 200px;">ip: <span style="font-weight: bold;" >' . $res['ip'] . '</span></td>
					<td style="width: 200px;">mac: <span style="font-weight: bold;" >' . $res['mac'] . '</span></td>
					<td style="width: 200px;">სტატუსი: <span style="font-weight: bold;" >' . $res['status'] . '</span></td>
				</tr>
	
			</table>
			<fieldset  '.$check.'>
		    	<legend>screenshot</legend>
					<img id="image" width="1024" height="768" src="http://192.168.11.100/crystalmonitoring/index.php?act=getimage&mac='.$res[mac].'&refresh='. time().'">
				  	<input type="hidden" id="mac" value="' . $res['mac'] . '" />
			</fieldset>
		</div>
	';
	return $data;
}
?>
